package it.rap.applications.birt.report.accounts.core;


import it.rap.applications.birt.report.accounts.internal.Activator;

import java.io.InputStream;
import java.net.URL;

import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.report.engine.api.EngineConfig;
import org.eclipse.birt.report.engine.api.EngineException;
import org.eclipse.birt.report.engine.api.IReportEngine;
import org.eclipse.birt.report.engine.api.IReportEngineFactory;
import org.eclipse.birt.report.engine.api.IReportRunnable;
import org.eclipse.birt.report.engine.api.IRunAndRenderTask;
import org.eclipse.birt.report.engine.api.RenderOption;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Path;
import org.eclipse.rap.rwt.RWT;
import org.osgi.framework.Bundle;
import org.osgi.framework.FrameworkUtil;

public abstract class BirtReportManager {

	//  private static final String REPORT_NAME = "TopNPercent.rptdesign";
	//  private static final String REPORT_NAME = "reportTest.rptdesign";
	//  private static final String REPORT_NAME = "uses_library.rptdesign";
	//  private static final String REPORT_NAME = "uses_localization.rptdesign";

	private static final String REPORT_DIRECTORY = "reports/";
	  
	private IReportEngine engine = null;
	private EngineConfig config = null;
	private IReportRunnable design = null;
	private IRunAndRenderTask task;
	private String year = null;
	private String reportName;

	public BirtReportManager(String reportName) {
		this.reportName = reportName;
	}

	protected void runReport( RenderOption renderOptions ) {
	    task.setRenderOption( renderOptions );
	    if ( year != null && !"".equals( year )   ){
	    	task.setParameter("anno", year, "Anno di riferimento");
	    } else {
	    	task.setParameter("anno", "2012", "Anno di riferimento");
	    }
	    try {
	      task.run();
	    } catch( EngineException e ) {
	      e.printStackTrace();
	    }
	    task.close();
	    engine.destroy();
	  }

	  protected void createReport() throws Exception {
		  createReport(null);
	  }
	  protected void createReport(String year) throws Exception {
		 this.year = year;
		 config = new EngineConfig();
		 IReportEngineFactory factory = ( IReportEngineFactory )Platform.createFactoryObject( IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY );
		 engine = factory.createReportEngine( config );
		 InputStream fs = null;
		 try {
			 Bundle bundle = org.eclipse.core.runtime.Platform.getBundle( Activator.BUNDLE_ID );
			 URL resourceurl = FileLocator.find( bundle, new Path( "/" ), null );
			 String myresourcepath = FileLocator.toFileURL( resourceurl ).getPath();
			 config.setResourcePath( myresourcepath );
			 //URL url = ReportActivator.getDefault().getBundle().getResource( REPORT_DIRECTORY + REPORT_NAME );
			 URL url = FrameworkUtil.getBundle( getClass() ).getResource( REPORT_DIRECTORY + this.reportName );
			 fs = url.openStream();
			 design = engine.openReportDesign( fs );
		 } finally {
			 if(fs != null) {
				 fs.close();
			 }
		 }
		 task = engine.createRunAndRenderTask( design );
		 task.setLocale( RWT.getLocale() );
	  }

	  public abstract String doReport() throws Exception;

	public String getReportName() {
		return reportName;
	}

	public void setReportName(String reportName) {
		this.reportName = reportName;
	}
}
